// packageC/micro_communities/microAttention/microAttention.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    bannerindex: 1,
    info: {},
    mainCommentShow: false,
    maincomment: "",
    uName: null, //当前用户名称，用于发表评论时
    commentId: null,
    activeTid: null, //当前点击的帖子id
    isUnsubscribe: false, //取消关注弹窗
    UnsubscribeId: null, //要取消关注的用户id
    //more
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数

    isNOMore: false, //没有更多文本显示
    imgPath: app.globalData.imgPath
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.getFollowTrick");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          res.data.data.forEach((val)=>{
            val.created_at = that.timeSlice(val.created_at);
          });
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
            uName: res.data.uName,
            info: res.data.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  showUnsubscribe(e) {
    let stick_id = e.currentTarget.dataset.stickid;
    this.setData({
      UnsubscribeId : stick_id,
      isUnsubscribe: true
    });
  },
  isCoverLayer() {
    this.setData({
      mainCommentShow: false,
      isUnsubscribe: false
    });
  },
  UnsubscribeMethob() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.user.delFollow");
    let dataJson = {
      uid: this.data.UnsubscribeId
    };
    app._getNetWork({
      url: urlStr,
      data: dataJson,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          wx.showToast({
            title: "已取消关注",
            icon: 'none',
            duration: 1500
          });
          that.setData({
            isUnsubscribe: false,
          });
          that.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toEdit() {
    wx.navigateTo({
      url: '/packageC/micro_communities/microRelease/microRelease'
    });
  },
  gotoMicrocommentdetails(e) {
    let stick_id = e.currentTarget.dataset.stickid;
    console.log(stick_id,"stick_id");
    wx.navigateTo({
      url: '/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=' + stick_id
    });
  },
  selectLike(e) {
    let item = e.currentTarget.dataset.item;
    let stick_id = item.id;
    let status = item.is_praise;
    let ind = e.currentTarget.dataset.index;
    let that = this;
    if(status == 1) {
      let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.cancelPraise");
      let dataJson = {
        tid: stick_id
      };
      app._getNetWork({
        url: urlStr,
        data: dataJson,
        success: function (resdata) {
          let res = resdata.data;
          if (res.result === 1) {
            wx.showToast({
              title: "取消点赞",
              icon: 'none',
              duration: 1500
            });
            that.setData({
              [`info[${ind}].is_praise`]: 0
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }

        },
        fail: function (res) {
          console.log(res);
        }
      });
    } else {
      let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.givePraise");
      let dataJson = {
        tid: stick_id
      };
      app._getNetWork({
        url: urlStr,
        data: dataJson,
        success: function (resdata) {
          let res = resdata.data;
          if (res.result === 1) {
            wx.showToast({
              title: "点赞成功",
              icon: 'none',
              duration: 1500
            });
            that.setData({
              [`info[${ind}].is_praise`]: 1
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }

        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  sidingsImg(e) {
    let index = e.detail.current;
    this.setData({
      bannerindex : index+1
    });
  },
  maincommentinp(e) {
    let val = e.detail.value;
    this.setData({
      maincomment: val
    });
  },
  subMainCommentBtn(e) {
    let id = e.currentTarget.dataset.id;
    let ind = e.currentTarget.dataset.ind;
    this.setData({
      commentId: id,
      maincomment: "",
      activeTid: ind,
      mainCommentShow: true
    });
  },
  subMainBtn() {
    if (!this.data.maincomment) {
      wx.showToast({
        title: "请输入你的精彩评论",
        icon: 'none',
        duration: 1500
      });
      return;
    }
    this.setData({
      mainCommentShow: false
    });
    this.getReplyComments();
  },
  getReplyComments() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.comments");
    let dataJson = {
      tid: that.data.commentId,
      content: that.data.maincomment
    };
    app._postNetWork({
      url: urlStr,
      data: dataJson,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.data.info[that.data.activeTid].comment.unshift({
            content: that.data.maincomment,
            uname: that.data.uName
          });
          that.setData({
            info : that.data.info
          });
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  gotoHome(e) {
    let micuid = e.currentTarget.dataset.micuid;
    wx.redirectTo({
      url: '/packageC/micro_communities/microhomepage/microhomepage?micuid=' + micuid
    });
  },
  getMoreData() {
    let that = this;
    that.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.getFollowTrick");
      let dataJson = {
        page: that.data.page
      };
      app._getNetWork({
        url: urlStr,
        data: dataJson,
        success: function (resdata) {
          let res = resdata.data;
          that.setData({
            isLoadMore: true
          });
          if (res.result === 1) {
            let myData = res.data.data;
            myData.forEach((val)=>{
              val.created_at = that.timeSlice(val.created_at);
            });
            that.setData({
              info: that.data.info.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }

        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  timeSlice: function(setdateTimeStamp) {
    let minute = 1000 * 60;
    let hour = minute * 60;
    let day = hour * 24;
    // let halfamonth = day * 15;
    let month = day * 30;

    let dateTimeStamp = new Date(setdateTimeStamp);
    let now = new Date().getTime();
    let diffValue = now - dateTimeStamp;
    if (diffValue < 0) {
      //若日期不符则弹出窗口告之
      return "结束日期不能小于开始日期！";
    }
    let monthC = diffValue / month;
    let weekC = diffValue / (7 * day);
    let dayC = diffValue / day;
    let hourC = diffValue / hour;
    let minC = diffValue / minute;
    let result = null;
    if (monthC >= 1 && monthC <= 3) {
      result = setdateTimeStamp.substr(5, 11);
    } else if (weekC >= 1 && weekC <= 3) {
      result = setdateTimeStamp.substr(5, 11);
    } else if (dayC >= 1 && dayC <= 6) {
      result = setdateTimeStamp.substr(5, 11);
    } else if (hourC >= 1 && hourC <= 23) {
      result = parseInt(hourC) + "小时前";
    } else if (minC >= 1 && minC <= 59) {
      result = "今天" + parseInt(minC) + "分钟前";
    } else if (diffValue >= 0 && diffValue <= minute) {
      result = "刚刚";
    } else result = setdateTimeStamp.substr(5, 11);
    return result;
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
  onShareTimeline:function(){},
});
